package com.xal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xal.mapper.StudentMapper;
import com.xal.pojo.entiey.Student;
import com.xal.pojo.vo.StudnetVo;
import com.xal.service.StudentService;
import com.xal.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: Swagger2
 * @author: 邢奥林
 * @create: 2023-04-12 19:20
 **/
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Result findByPage(Integer current, Integer pageSize, StudnetVo studnetVo) {
        Result result=new Result();
        IPage<Student> page=new Page<>(current,pageSize);
        QueryWrapper<Student> wrapper=new QueryWrapper<>();

        if (studnetVo.getSname()!=null){
            wrapper.like("sname",studnetVo.getSname());
            result.getCode();
            result.getMsg();
            result.getData();
            System.out.println(result);

        }
        if (studnetVo.getSage()!=null && studnetVo.getSage()!=0){
            wrapper.ge("sage",studnetVo.getSage());
        }
        if(studnetVo.getSid()!=null && studnetVo.getSid()!=0){
            wrapper.ge("sid",studnetVo.getSid());
        }
        IPage<Student> byPage = studentMapper.findByPage(page, wrapper);
        return new Result(200,"查询成功",byPage);
    }

    @Override
    public Result findById(Integer sid) {
        Student byId = studentMapper.findById(sid);
        return new Result(200,"查询成功",byId);
    }

    @Override
    public Result findAll() {
        List<Student> all = studentMapper.findAll();
        return new Result(200,"查询成功",all);

    }

    @Override
    public Result insert(Student student) {
        int insert = studentMapper.insert(student);
        return new Result(200,"添加成功",insert);
    }

    @Override
    public Result update(Student student) {
        int update = studentMapper.update(student);
        if(update>0){
            return new Result(200,"修改成功",update);
        }else {
            return new Result(500,"修改成功",update);
        }

        }


    @Override
    public Result delete(Integer sid) {
        int delete = studentMapper.delete(sid);
        if(delete>0){
            return new Result(200,"删除成功",delete);
        }else {
            return new Result(500,"删除成功",delete);
        }

    }

    @Override
    public Result Bachdelete(List<Student> students) {
       students.stream().map(item->item.getSid()).collect(Collectors.toList());
        int i = studentMapper.deleteBatchIds(students);
        if(i>0) {
            return new Result(200,"删除成功",i);
        }else {
            return new Result(500,"修改成功",i);
        }

    }


}
